
package com.wyp.didishige.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期 - 工具
 */
public class DateUtil {

	/**
	 * 获取时间戳
	 * 
	 * @param dateNow
	 * @return
	 */
	public static long getTimestamp(Date dateNow) {

        return System.currentTimeMillis() / 1000;
		/*if (dateNow == null) {
			return 0;
		}

		long timestamp = 0;
		try {
			SimpleDateFormat fm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			Date date = fm.parse("1970/01/01 08:00:00");
			timestamp = dateNow.getTime() - date.getTime();
			if (timestamp < 0) {
				timestamp = 0;
			}
		} catch (ParseException e) {
			timestamp = 0;
		}

		return timestamp;*/
	}

	/**
	 * 获取时间
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getDate(long timestamp, String dateFormat) {
		if (timestamp < 0) {
			return "";
		}

		String result;
		Date date = new Date(timestamp);
		SimpleDateFormat fm = new SimpleDateFormat(dateFormat);
		result = fm.format(date);

		return result;
	}

	/**
	 * 根据时间戳获取年龄
	 * @param date 出身日清时间戳
	 * @return 年龄
	 */
	public static int getAge(String date){
		Date birthDay = getDate(date);
		if (birthDay ==null) return 0;
		Calendar calendar=Calendar.getInstance();
		Calendar birthCal=Calendar.getInstance();
		birthCal.setTime(birthDay);
		int currentYear = calendar.get(Calendar.YEAR);
		int birthYear = birthCal.get(Calendar.YEAR);
		int age = currentYear-birthYear;
		if (age<0){
			return 0;
		}else{
			return age;
		}
	}

	/**
	 *
	 * @return
	 */
	public static Date getDate(String date){
		if (StringUtils.isNullOrEmpty(date)) return null;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取时间间隔
	 * 
	 * @param beginTimestamp
	 * @param endTimestamp
	 * @return
	 */
	public static String getDateSpan(long beginTimestamp, long endTimestamp) {
		if (beginTimestamp < 0 || endTimestamp < 0) {
			return "";
		}

		String result = "";
		int span = 0;
		long timeSpan = endTimestamp - beginTimestamp;
		final int weekMs = 60 * 60 * 24 * 7;
		final int dayMs = 60 * 60 * 24;
		final int hourMs = 60 * 60;
		final int minutesMs = 60;
		final int secondMs = 1;

		if (timeSpan / weekMs < 4 && timeSpan / weekMs > 0) {
			span = (int) (timeSpan / weekMs);
			result = span + "周前";
		} else if (timeSpan / dayMs < 7 && timeSpan / dayMs > 0) {
			span = (int) (timeSpan / dayMs);
			result = span + "天前";
		} else if (timeSpan / hourMs < 24 && timeSpan / hourMs > 0) {
			span = (int) (timeSpan / hourMs);
			result = span + "小时前";
		} else if (timeSpan / minutesMs < 60 && timeSpan / minutesMs > 0) {
			span = (int) (timeSpan / minutesMs);
			result = span + "分钟前";
		}
		else if (timeSpan / secondMs < 60 && timeSpan / secondMs > 0) {
			span = (int) (timeSpan / secondMs);
			result = span + "秒前";
		}
		else if (timeSpan / secondMs <= 0) {
			result = "刚刚";
		} else {
			Calendar endCal = Calendar.getInstance();
			endCal.setTimeInMillis(endTimestamp);
			Calendar beginCal = Calendar.getInstance();
			beginCal.setTimeInMillis(beginTimestamp);

			SimpleDateFormat fm;
			// 同年
			if (endCal.get(Calendar.YEAR) == beginCal.get(Calendar.YEAR)) {
				fm = new SimpleDateFormat("MM-dd");
			}
			// 非同年
			else {
				fm = new SimpleDateFormat("yyyy-MM-dd");
			}
			result = fm.format(new Date(beginTimestamp));

		}

		return result;
	}
}
